package com.ktjy.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ktjy.entity.SysRight;
import com.ktjy.service.ISysRightService;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.crazycake.shiro.RedisCacheManager;
import org.crazycake.shiro.RedisManager;
import org.crazycake.shiro.RedisSessionDAO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lqm
 * @ClassName ShiroConfig
 * @description: TODO
 */
@Configuration
public class ShiroConfig {


    @Resource
    ISysRightService sysRightService;

    @Value("${spring.redis.password}")
    private String password;

    @Bean
    public Realm realm() {
        CustoRealm realm = new CustoRealm();

        realm.setCacheManager(redisCacheManager());
        //设置授权缓存
        realm.setAuthorizationCachingEnabled(true);
        //设置认证缓存
        realm.setAuthenticationCachingEnabled(true);
        return realm;
    }




    @Bean
    public RedisCacheManager redisCacheManager(){
        RedisCacheManager cacheManager=new RedisCacheManager();
        cacheManager.setRedisManager(redisManager());
        //设置缓存id字段
        cacheManager.setPrincipalIdFieldName("usrId");
//        cacheManager.setExpire(30);
        return cacheManager;

    }

    @Bean
    public DefaultWebSecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(realm());
        securityManager.setSessionManager(sessionManager());
        securityManager.setCacheManager(redisCacheManager());
        return securityManager;
    }
//
    @Bean
    public RedisManager redisManager() {
        System.out.println("交给redis");
        RedisManager redisManager = new RedisManager();
        redisManager.setPassword(password);
        return redisManager;
    }
//
//
//
    @Bean
    public SessionManager sessionManager() {
        DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
        sessionManager.setSessionDAO(sessionDao());

        //解决地址栏出现sessionId问题
        sessionManager.setSessionIdUrlRewritingEnabled(false);
        return sessionManager;
    }
//
    @Bean
    SessionDAO sessionDao() {
        RedisSessionDAO redisSessionDAO = new RedisSessionDAO();
//        redisSessionDAO.setExpire(30);//设置session有效期30秒  默认有效1800秒
        redisSessionDAO.setRedisManager(redisManager());
//        System.out.println(redisSessionDAO.getSessionInMemoryEnabled());
//        System.out.println(redisSessionDAO.getSessionInMemoryTimeout());
        System.out.println("是否使用redis缓存");
        return redisSessionDAO;
    }


    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {

        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();

        shiroFilterFactoryBean.setSecurityManager(securityManager);
        shiroFilterFactoryBean.setLoginUrl("/");
        shiroFilterFactoryBean.setUnauthorizedUrl("/403");
        shiroFilterFactoryBean.setSuccessUrl("/main");

        //哪些请求需要权限验证，哪些请求可以匿名访问

        //key 标识资源路径   value  shiro内置的过滤规则


        Map<String, String> resourcesMap = new LinkedHashMap<>();
        resourcesMap.put("/doLogin", "anon");
        resourcesMap.put("/static/**", "anon");
        resourcesMap.put("/css/**", "anon");
        resourcesMap.put("/fonts/**", "anon");
        resourcesMap.put("/images/**", "anon");
        resourcesMap.put("/js/**", "anon");
        resourcesMap.put("/localcss/**", "anon");
        resourcesMap.put("/localjs/**", "anon");
        resourcesMap.put("/logout", "logout");


//

        List<SysRight> rightList = sysRightService.list();
        for (SysRight right : rightList) {
            if (StringUtils.isNotEmpty(right.getRightUrl().trim())) {
                resourcesMap.put(right.getRightUrl(), "perms[" + right.getRightCode() + "]");
            }
        }


        resourcesMap.put("/**", "authc");

        shiroFilterFactoryBean.setFilterChainDefinitionMap(resourcesMap);

        return shiroFilterFactoryBean;
    }


    @Bean
    public ShiroDialect shiroDialect() {
        ShiroDialect shiroDialect = new ShiroDialect();
        return shiroDialect;
    }


}
